#ifndef MODULE_WINDOW_H
#define MODULE_WINDOW_H

#include <QFrame>
#include <QMouseEvent>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QGraphicsTextItem>
#include <QGraphicsSceneEvent>

#include "module/property/module_port_property.h"
#include "module/property/module_window_property.h"
#include "module/property/module_propertys.h"

#include "module/modulefileconfig.h"


namespace Ui {
class module_window;
}

#define MODULE_DEFAULT_xRadius 25
#define MODULE_DEFAULT_yRadius 25
#define MODULE_DEFAULT_MARGIN 60

enum ModuleWindowType{ModuleType, PortType};

///---------------------------------------------------------------

class Module : public QObject, public QGraphicsItem
{
    Q_OBJECT
public:
    Module(QSizeF size, QGraphicsItem *parent = nullptr);
    Module(QGraphicsItem *parent = nullptr);
    ~Module();

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    QRectF boundingRect() const;

    void setName(QString name, QColor color);



signals:
    void send_modulePressSignal(QGraphicsItem *, ModuleWindowType type);
    void send_moduleDoubleClickSignal(Module *);

protected:
    QGraphicsTextItem *Name;
    qreal module_width, module_height;

    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);

};

class ChildModule : public Module
{
    Q_OBJECT
public:
    ChildModule(QSizeF size, QGraphicsItem *parent);
    ~ChildModule();

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

protected:
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);

    QVariant itemChange(GraphicsItemChange change, const QVariant &value);

    QPainterPath shape() const;

private:
    Module *module_parent;

};

class ModulePort : public Module
{
    Q_OBJECT
public:
    ModulePort(QGraphicsItem *parent);
    ~ModulePort();

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

    qreal compute_port_y(int num, int sum);

signals:
    void send_portPressSignal(QGraphicsItem *, ModuleWindowType type);

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void setRect();

private:
    QGraphicsItem *module;
};

///---------------------------------------------------------------

class ModuleLine : public QGraphicsLineItem
{
public:
    ModuleLine(QGraphicsItem *startItem, QGraphicsItem *endItem);
    ~ModuleLine();

    virtual QRectF boundingRect() const;
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

private:
    QGraphicsItem *_startItem, *_endItem;
};


///---------------------------------------------------------------

class module_window : public QFrame
{
    Q_OBJECT

public:

    explicit module_window(Module_Data data, QWidget *parent = nullptr);

    void set_module_name(QString name);

    void add_child_module(Module_Data data, QPointF point);

    void init();

    ///---------------------------------------------------------------

    QString get_module_name();
    QString get_module_type();
    QString get_module_class();
    QString code_generate();
    ~module_window();

signals:
    void changeLableName(QString name);

public slots:
    void setLableName(QString name);

    void make_property(QGraphicsItem *module, ModuleWindowType type);
    void make_code_window(Module *module);

protected:
//    void mousePressEvent(QMouseEvent *event) override;

private:
    Ui::module_window *ui;

    Module_Data module_data;

    QRect window_rect;

    QGraphicsScene *scene = new QGraphicsScene(this);

    void init_module();
    void init_childModule(Module *module, QList<Module_Data> *data_list);
    void init_port(Module *module, Module_Data data); // 创建端口

    struct _Module_Map {
        QGraphicsItem *module;
        Module_Data data;
    };

    Module *master_module;

    QMap<QGraphicsItem *, Module_Data> module_map;
    QMap<QGraphicsItem *, PortAttribute> module_port_map;

};

///---------------------------------------------------------------


#endif // MODULE_WINDOW_H
